function studentGrid()
{
    $('#studentGrid').jqGrid({
                url: siteUrl + 'student/get_students',
                mtype: 'post',
                datatype: 'json',
                colNames: ['ID', 'User No.','Name','Status', 'Operations'],
                colModel: [
                {name: 'user_id', index: 'user_id', hidden: true, key: true},
                {name: 'user_no', index: 'user_no'},
                {name: 'name', index: 'name'},
                {name: 'status', index: 'status'},
                {name: 'ops', index: 'ops', align: 'center', sortable: false, formatter: instructorOperations}
                ],


            autowidth: true,
            height: '200',
            pager: $('#pagination'),
            sortname: 'user_id',
            sortorder: 'asc',
            viewrecords: true,
            rownumbers: true,
            ondblClickRow: function(rowId, iRow, iCol, e) {
                window.location = siteUrl + 'student/view/' + rowId;
            }
    });
}
function instructorOperations(cell, opts, rowObj)
{
    var id = opts.rowId;
    var msg = "Are you sure you want to set to inactive the selected record?";
    var edit = '<a href="' + siteUrl + 'student/edit/' + id + '" title="Update" class="icon icon-update"></a>';

    var del = '<a href="javascript:$.confirmDelete(' + id + ', \'deleteStudent\', \'Student - De-activate Record\', \'\', \''+ msg +'\')" title="De-activate" class="icon icon-delete"></a>';
    del = (rowObj[3] == 'Inactive') ? '<span class = "icon" ></span>' : del;
    return edit+del;
}

function studentRules()
{


    $('#user_form').validate({
        debug: true,
        rules: {
            user_no: {
                required: true
            },
            course: {
                required: true
            },
            fname: {
                required: true
            },
            lname: {
                required: true
            },
            contact_no: {
                required: true
            },
            contact_person: {
                required: true
            },
            contact_no: {
                required: true
            },
            address: {
                required: true
            },
            email: {
                required: true,
                email: true,
                remote: {
                    url: siteUrl + 'user/unique',
                    type: 'post',
                    data: {
                        field: 'email',
                        user_id: function() {
                            return $('#user_id').val();
                        }
                    }
                }
            }
        },
        messages: {
            user_no: {
                required: $.Msgs('required')
            },
            course: {
                required: $.Msgs('required')
            },
            fname: {
                required: $.Msgs('required')
            },
            lname: {
                required: $.Msgs('required')
            },
            contact_no: {
                required: $.Msgs('required')
            },
            contact_person: {
                required: $.Msgs('required')
            },
            contact_no: {
                required: $.Msgs('required')
            },
            address: {
                required: $.Msgs('required')
            },
            email: {
                required: $.Msgs('required'),
                email: $.Msgs('validEmail'),
                remote: $.Msgs('unique')
            }
        },
        onkeyup: false,
        focusInvalid: true,
        errorElement: 'span',
        errorPlacement: function(error, element) {
            $(element).next('.icon-error').remove();
            $(element).parent().append('<span class="icon icon-error"></span>');
            var icon = $(element).next('span');

            icon.qtip({
                content: $(error).text(),
                position: {
                    corner: {
                        target: 'rightTop',
                        tooltip: 'leftBottom'
                    }
                },
                hide: {
                    when: 'inactive',
                    delay: 1500
                },
                style: 'fielderror'
            });
        },
        highlight: function(element, errorClass) {
            $(element).parent().addClass('ui-state-error ui-corner-all');
        },
        unhighlight: function(element, errorClass) {
            $(element).next('.icon-error').remove();
            $(element).parent().removeClass('ui-state-error ui-corner-all');
        },
        submitHandler: function(form) {
            saveStudent();
        }
    });

}

function saveStudent(){
    $.loading('Saving Student Record');
    
    $.ajax({
        url: siteUrl + 'student/submit',
        type: 'post',
        data: $('#user_form').serialize(),
        success: function(response) {
            if (response)
            {
                window.location = siteUrl + 'student';
            }
        }
    });
}

function deleteStudent(contentId)
{
    $.loading('Student - De-activating Record');

    $.ajax({
        url: siteUrl + 'student/set_to_inactive/' + contentId,
        type: 'post',
        dataType: 'json',
        success: function(response) {
            if (response == true)
            {
                window.location = siteUrl + 'student';
            }

        }
    });
}